<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Doctor extends Controller
{
    private $_model;
    private $title_field = [
    ];
    public $pagesize = 10;

    public $allTypeLists = array();

    public function _initialize()
    {
        $this->_model = new \app\admin\model\HospDoctor();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        $this->doctor_level = config("HOSPTIAL.doctor_level");
        $this->doctor_level = changeArrayKey($this->doctor_level, 'id');

        $this->assign('doctor_level', $this->doctor_level);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
        $this->getAllHospital();

        //获取科室
        $this->hospSection = getHospSection(0);//获取科室
        $this->hospSection = changeArrayKey($this->hospSection, 'id');
        $this->assign('hospSection', $this->hospSection);
    }

    private function getAllHospital()
    {
        $lists = (new \app\admin\model\HospHospital())->getDataForWheres(['is_delete' => 0]);
        $lists = changeArrayKey($lists, 'id');
        $this->assign('hosptialLists', $lists);
    }

    public function shangData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 0), $id);
        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }

    public function xiaData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 1), $id);

        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }


    public function lists()
    {
        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth


        $ser_hot = trim(input("request.ser_hot"));//auth
        $ser_stats = trim(input("request.ser_stats"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }

        if ($ser_hot == '0' || $ser_hot == '1') {
            $where['hot'] = $ser_hot;
        }

        $where['is_delete'] = '0';

        $lists = $this->_model->lists($where, $this->pagesize);

        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');
        $lists['data'] = filterHtmlForLists($lists['data']);
        $lists['data'] = (new \app\admin\mapper\Common())->listMapper($lists['data']);

        $this->assign('lists', $lists);
        $this->assign('request', input("request."));
        return view('lists');
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('is_delete' => 1), $id);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    private function addUser($tel)
    {
        $userInfo = getUserInfoForUname($tel);
        $model = new \app\admin\model\User();
        if (empty($userInfo))//添加用户
        {
            return $model->add(array('uname' => $tel,'is_doctor'=>1));
        } else {
            if (empty($userInfo['is_doctor'])) {
                $model->updateDataForWhere(['is_doctor' => 1], ['id' => $userInfo['id']]);
            }
            return $userInfo['id'];
        }
    }

    public function addData()
    {
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            if (empty($data1['tel'])) {
                $return = ['err' => 1, 'msg' => '手机号不能未空'];
                return json($return);
            }
            $tel = $data1['tel'];
            //检查用户
            $doctor_id = $this->addUser($tel);
            if (empty($doctor_id)) {
                $return = ['err' => 1, 'msg' => '用户添加失败'];
                return json($return);
            }
            $oldInfo = (new \app\admin\model\HospDoctor())->getDataForWhere(['tel'=>$tel]);
            if($oldInfo)
            {
                $return = ['err' => 1, 'msg' => '该手机号已经存在'];
                return json($return);
            }


            //获取配置中医生等级配置
            $doctorLevelConfig = config("HOSPTIAL.doctor_level");
            //获取配置金额
            $data2 = array(
                'uid' => $doctor_id,
                'ctime' => time(),
                'utime' => time(),
                'service_price'=>$doctorLevelConfig[0]['money'],
                'service_price_now'=>$doctorLevelConfig[0]['money'],
                'fast_service_price_now'=>$doctorLevelConfig[0]['fast_money'],
                'fast_service_price'=>$doctorLevelConfig[0]['fast_money'],
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->add($data);
            if ($res) {
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }
        $title = "添加";

        $this->assign('title', $title);
        return view('addData');
    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $w = array(
                'id' => $id
            );
            $data2 = array(
                'utime' => time()
            );
            $data = array_merge($data1, $data2);


            $res = $this->_model->updateDataForWhere($data, $w);

            if ($res || ($res === 0)) {
                $this->addUser($info['tel']);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


        $this->assign('info', $info);
        //设置全局信息
        $title = "编辑";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');

    }

    public function hospconfig()
    {

        $sys_opt = trim(input("request.sys_opt"));//auth
//获取医生等级配置
        $levelList = config("HOSPTIAL.doctor_level");
        $levelList = changeArrayKey($levelList,'id');

        if ($sys_opt == "editconfigdata") {
            $data = $_REQUEST['data'];
            foreach ($data as $k => $v) {
                $model = new \app\admin\model\HospSysconfig();
                $model->updateDataForWhere(['varvalue' => $v], ['varname' => $k]);
            }
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);

        } else if ($sys_opt == "editleveldata") {
            $data = $_REQUEST['data'];
            $saveData = [];
            foreach ($data as $key => $val) {
                $tempKey = explode("__", $key);
                $levelList[$tempKey[0]][ $tempKey[1]]=$val;
            }
            foreach ($levelList as $key=>$val){
                $saveData[]=$val;
            }
            $hospConfig = config("HOSPTIAL");
            $hospConfig['doctor_level'] = $saveData;
            $file = dirname(realpath(APP_PATH));
            $file = $file . DS . "application" . DS . "hosptial.config.php";
            $text='<?php return '.var_export($hospConfig,true).';';
            file_put_contents($file, $text);
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);

        }
        //获取系统配置
        $model = new \app\admin\model\HospSysconfig();
        $list = $model->getDataForWheres([], "*", "id desc");

        $this->assign('levelList', $levelList);
        $this->assign('list', $list);
        return view('hospconfig');
    }
}